package com.zl.smartmall.library.account.vo;

import java.util.List;

import org.json.JSONException;

import com.zl.smartmall.library.account.po.UserInfo;

import net.tsz.afinal.FinalDb;
import android.content.Context;

public class UserInfoDao {

	private FinalDb mFinalDb;
	
	public UserInfoDao(Context context){
		mFinalDb = FinalDb.create(context);
	}
	
	public void insert(UserInfo model) throws JSONException {
		synchronized (UserInfoDao.class) {
			mFinalDb.save(model);
		}

	}
	
	public void delete() {
		synchronized (UserInfoDao.class) {
			mFinalDb.deleteByWhere(UserInfo.class, null);
		}
	}
	
	public void delete(int uid) {
		synchronized (UserInfoDao.class) {
			mFinalDb.deleteByWhere(UserInfo.class, "uid=" + uid);
		}
	}

	public UserInfo query() {
		synchronized (UserInfoDao.class) {
			List<UserInfo> models = mFinalDb.findAll(UserInfo.class);
			if(models != null && models.size() > 0) {
				return models.get(0);
			}
			return null;
		}
	}
	
	public UserInfo query(int uid) {
		synchronized (UserInfoDao.class) {
			UserInfo userInfo = mFinalDb.getUnique(UserInfo.class, "uid=" + uid);
			return userInfo;
		}
	}
	
}
